package com.goddess.yeecolor.base;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;

import com.goddess.yeecolor.tools.Constans;

/**
 * Created by yeecolor on 17/1/3.
 */
public abstract class BaseActivity extends FragmentActivity {
    /**
     * 需要进行检测的权限数组
     */
    public static String[] needPermissions = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS
    };

    /**
     * 点击事件
     */
    public abstract void initOnClck();

    /**
     * 初始化
     */
    public  abstract void initFindView();

    /**
     * 初始化数据
     */
    public abstract void initData();

    public static void setFiish(Activity context){
        context.finish();
        context.overridePendingTransition(0,0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(0,0);
    }

    public boolean hasPermissin(String...permissions){

        for (String permission:permissions){
            /**
             * 判断权限  检查权限
             */
            if (ContextCompat.checkSelfPermission(this,permission)!= PackageManager.PERMISSION_GRANTED){

                return false;
            }
        }
        return true;
    }

    /**
     * 权限请求
     */
    public void requestPermission(int code,String...permissions){
        ActivityCompat.requestPermissions(this,permissions,code);
    }
    /**
     * 权限回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case Constans.WRIGTE_CALL_EXTERNAL_CODE:
                doCall();
                break;
            case 4:
                break;
        }
    }


    /**
     * 权限处理
     */
    public void doCall(){

    }
}
